package com.imooc.pan.file.service.es.impl;

import com.imooc.pan.core.constants.RPanConstants;
import com.imooc.pan.file.context.FileSearchContext;
import com.imooc.pan.file.entity.RPanUserFile;
import com.imooc.pan.file.entity.es.FileMessageEs;
import com.imooc.pan.file.mapper.es.FileMessageEsMapper;
import com.imooc.pan.file.service.IUserFileService;
import com.imooc.pan.file.service.es.FileMessageEsService;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Janson
 * @Description Mysql es 文件数据同步接口
 * @Date 2023/7/28
 */

@Service
public class FileMessageEsServiceImpl implements FileMessageEsService {
    @Autowired
    private FileMessageEsMapper fileMessageEsMapper;
    
    @Autowired
    private IUserFileService iUserFileService;
    @Override
    public void insertFiles(){
       /*List<RPanUserFile> list = iUserFileService.list();
       list.forEach(r->{
           FileMessageEs fileMessageEs = new FileMessageEs();
           BeanUtils.copyProperties(r,fileMessageEs);
           // fileMessageEs.setId(IdUtil.simpleUUID());
           fileMessageEs.setParentFilename("我是你爹");
           Integer insert = fileMessageEsMapper.insert(fileMessageEs);
       });*/
        List<RPanUserFile> list = iUserFileService.list();
        RPanUserFile userFile = list.get(1);
        FileMessageEs fileMessageEs = new FileMessageEs();
        // fileMessageEs.setFileId(userFile.getFileId());
        fileMessageEs.setFilename("我是你二爹");
        LambdaEsQueryWrapper<FileMessageEs> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(FileMessageEs::getFileId,userFile.getFileId());
        fileMessageEsMapper.update(fileMessageEs,wrapper);
        
        System.out.println("插入成功");
       
       
   }
    
    /**
     * 全文检索 ES 接口
     * @param context
     * @return
     */
    @Override
    public List<FileMessageEs> selectFilesHighlight(FileSearchContext context){
        LambdaEsQueryWrapper<FileMessageEs> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.match(FileMessageEs::getFilename,context.getKeyword());
        wrapper.eq(FileMessageEs::getUserId, context.getUserId());
        List<FileMessageEs> fileMessageEsList = fileMessageEsMapper.selectList(wrapper);
        return fileMessageEsList;
    }
    
    /**
     * 测试使用接口
     * @return
     */
    @Override
    @Deprecated
    public List<FileMessageEs> findHightLight() {
        LambdaEsQueryWrapper<FileMessageEs> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.match(FileMessageEs::getFilename,"文件");
        // 没有删除的文件（文件夹）
        wrapper.eq(FileMessageEs::getDelFlag, RPanConstants.ZERO_INT);
        List<FileMessageEs> fileMessageEsList = fileMessageEsMapper.selectList(wrapper);
        return fileMessageEsList;
    }
}
